#include "SoundLoader.h"
#include "SimpleAudioEngine.h"
#include "PlayerData.h"

using namespace cocos2d;

// Android performs better in .OGG audio files

const char* BACKGROUND_MUSIC_FILE 	= "Game/Sound/Background_Music.mp3";
const char* CAT_INJURED_FILE	 	= "Game/Sound/Cat_Injured.wav";
const char* CAT_DEATH_FILE 			= "Game/Sound/Cat_Death.ogg";

const char* CHAR_INJURED_FILE	 	= "Game/Sound/Character_Injured.ogg";
const char* CHAR_DEATH_FILE 		= "Game/Sound/Character_Death.ogg";

const char* NINJA_SKILL_FILE 		= "Game/Sound/Ninja_Poof.ogg";
const char* WITCH_SKILL_FILE 		= "Game/Sound/Witch_Summon.ogg";

const char* WITCH_SHOOT_FILE 		= "Game/Sound/Witch_Shoot.ogg";

const char* SKILL_RAIN_FILE 		= "Game/Sound/Rain.ogg";
const char* SKILL_LIGHTNING_FILE 	= "Game/Sound/Zapped.wav";
const char* SKILL_HAMMER_FILE 		= "Game/Sound/Thor_Hammer.ogg";
const char* SKILL_TORNADO_FILE 		= "Game/Sound/Tornado.ogg";
const char* SKILL_METEOR_FILE 		= "Game/Sound/Meteor.ogg";

const char* SKILL_EARTHQUAKE_FILE 		= "Game/Sound/Earthquake.ogg";
const char* SKILL_EARTHQUAKE_END_FILE 	= "Game/Sound/Earthquake_End.ogg";

const char* LEVEL_COMPLETE_FILE 		= "Game/Sound/Level_Complete.ogg";

unsigned int SoundLoader::TORNADO_SOUND_ID = 0;
unsigned int SoundLoader::EARTHQUAKE_SOUND_ID = 0;

bool SoundLoader::CAT_DEATH = false;
bool SoundLoader::CAT_INJURED = false;
bool SoundLoader::NINJA_SKILL = false;
bool SoundLoader::WITCH_SKILL = false;
bool SoundLoader::IS_MUSIC_MUTE = false;
bool SoundLoader::IS_SOUND_EFFECT_MUTE = false;
bool SoundLoader::IS_MUSIC_PLAYING = false;

void SoundLoader::init()
{
	IS_MUSIC_MUTE = PlayerData::isMusicMute();
	IS_SOUND_EFFECT_MUTE = PlayerData::isSoundEffectMute();
}

void SoundLoader::playSound(EnumSoundType soundType)
{
	if(!IS_SOUND_EFFECT_MUTE)
	{
		switch (soundType)
		{
		case CAT_INJURED_SOUND:
			if(!CAT_INJURED)
			{
				CAT_INJURED = true;
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CAT_INJURED_FILE);
			}
			break;
		case CAT_DEATH_SOUND:
			if(!CAT_DEATH)
			{
				CAT_DEATH = true;
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CAT_DEATH_FILE);
			}
			break;
		case NINJA_SKILL_SOUND:
			if(!NINJA_SKILL)
			{
				NINJA_SKILL = true;
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(NINJA_SKILL_FILE);
			}
			break;
		case WITCH_SKILL_SOUND:
			if(!WITCH_SKILL)
			{
				WITCH_SKILL = true;
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(WITCH_SKILL_FILE);
			}
			break;
		case WITCH_SHOOT_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(WITCH_SHOOT_FILE);
			break;
		case CHAR_INJURED_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CHAR_INJURED_FILE);
			break;
		case CHAR_DEATH_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CHAR_DEATH_FILE);
			break;
		case SKILL_RAIN_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_RAIN_FILE);
			break;
		case SKILL_LIGHTNING_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_LIGHTNING_FILE);
			break;
		case SKILL_METEOR_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_METEOR_FILE);
			break;
		case SKILL_TORNADO_SOUND:
			TORNADO_SOUND_ID = CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_TORNADO_FILE, true);
			break;
		case SKILL_EARTHQUAKE_SOUND:
			EARTHQUAKE_SOUND_ID = CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_EARTHQUAKE_FILE);
			break;
		case SKILL_HAMMER_SOUND:
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_HAMMER_FILE);
			break;
		case BACKGROUND_MUSIC:
			if( !IS_MUSIC_MUTE )
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(BACKGROUND_MUSIC_FILE, true);
				IS_MUSIC_PLAYING = true;
			}
			break;
		case LEVEL_COMPLETE:
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(LEVEL_COMPLETE_FILE);
				break;
		case LEVEL_FAIL:
				break;
		default:
			break;
		}
	}
}

void SoundLoader::endSound(EnumSoundType soundType)
{
	switch (soundType)
	{
	case CAT_INJURED_SOUND:
		CAT_INJURED = false;
		break;
	case CAT_DEATH_SOUND:
		CAT_DEATH = false;
		break;
	case NINJA_SKILL_SOUND:
		NINJA_SKILL = false;
		break;
	case WITCH_SKILL_SOUND:
		WITCH_SKILL = false;
		break;
	case SKILL_TORNADO_SOUND:
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopEffect(TORNADO_SOUND_ID);
		break;
	case SKILL_EARTHQUAKE_SOUND:
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SKILL_EARTHQUAKE_END_FILE);
		break;
	case BACKGROUND_MUSIC:
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		IS_MUSIC_PLAYING = false;
		break;
	default:
		break;
	}
}

void SoundLoader::toggleSoundEffect()
{
	if( IS_SOUND_EFFECT_MUTE )
	{
		CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeAllEffects();
		IS_SOUND_EFFECT_MUTE = false;
	}
	else
	{
		CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseAllEffects();
		IS_SOUND_EFFECT_MUTE = true;
	}
	PlayerData::saveIsSoundEffectMute(IS_SOUND_EFFECT_MUTE);
}

void SoundLoader::toggleBackgroundMusic()
{
	if( !IS_MUSIC_MUTE )
	{
		CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
		IS_MUSIC_MUTE = true;
	}
	else
	{
		if( IS_MUSIC_PLAYING )
		{
			CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
		}
		else
		{
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(BACKGROUND_MUSIC_FILE, true);
			IS_MUSIC_PLAYING = true;
		}
		IS_MUSIC_MUTE = false;
	}
	PlayerData::saveIsMusicMute(IS_MUSIC_MUTE);
}

void SoundLoader::preloadSounds()
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(BACKGROUND_MUSIC_FILE);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CHAR_INJURED_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CHAR_DEATH_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CAT_INJURED_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CAT_DEATH_FILE);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(NINJA_SKILL_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(WITCH_SKILL_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(WITCH_SHOOT_FILE);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_RAIN_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_LIGHTNING_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_TORNADO_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_METEOR_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_EARTHQUAKE_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_EARTHQUAKE_END_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SKILL_HAMMER_FILE);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(LEVEL_COMPLETE_FILE);
}
